package com.itheima.controller;

import com.itheima.pojo.Result;
import com.itheima.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * Author: Zheng
 * Date:   2024/1/23  20:40
 * Package：com.itheima.controller
 * Description：文件上传模块
 * Version：1.0
 */
@Slf4j
@RestController
public class UploadController {
    @Autowired
    private AliOSSUtils aliOSSUtils;

    /*@PostMapping("/upload")
    public Result upload(String username, Integer age, MultipartFile image){
        log.info("文件上传，数据参数：{},{}",username,age);
        //获取原始文件名
        String originalFilename = image.getOriginalFilename();
        //获取扩展名
        String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
        //生成唯一文件名，UUID
        String uuid = UUID.randomUUID().toString();
        String fileName = uuid + extName;
        try {
            image.transferTo(new File("E:/images/"+fileName));
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error("上传失败~");
        }
        return Result.success();
    }*/
    /**
     * @description： 图片上传操作
     * @date：2024/1/26  10:49
     * @return：
     **/
    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException {

        log.info("图片上传操作。参数{}",image);
        String url = aliOSSUtils.upload(image);

        return Result.success(url);
    }

}
